﻿using LoadClient.YUAN.E;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoadClient.YUAN.B
{
    public class CommnadManager
    {

        public static string GetAddressStringByKeys(string key)
        {
            return FreeSqlDB.fsql00.Select<Command>().Where(a => a.CommandName.Equals(key)).ToOne().NodeString;
        }


        public static Command GetCommandByKeys(string key)
        {
            Command cmd = FreeSqlDB.fsql00.Select<Command>().Where(a => a.CommandName.Equals(key)).ToOne();
            return cmd == null ? new Command() : cmd;
        }

        public static bool InsertCommandRecord(CommandRecord cmdr)
        {
            return FreeSqlDB.fsql00.Insert(cmdr).ExecuteAffrows() > 0;
        }


        public static bool InsertCommandRecordFlag(CommandRecord cmdr)
        {
            return FreeSqlDB.fsql00.Insert(cmdr).ExecuteAffrows() > 0;
        }


        public static bool InsertCommandRecordList(List<CommandRecord> cmdrlist)
        {
            return FreeSqlDB.fsql00.Insert(cmdrlist).ExecuteAffrows() > 0;
        }

        public static bool GetGUID_OKResult(string startguid, string endguid)
        {

            try
            {
                int x1 = FreeSqlDB.fsql00.Select<CommandRecord>().Where(a => a.Guid.Equals(startguid)).ToOne().Id;
                int x2 = FreeSqlDB.fsql00.Select<CommandRecord>().Where(a => a.Guid.Equals(endguid)).ToOne().Id;
                List<CommandRecord> listcommand = FreeSqlDB.fsql00.Select<CommandRecord>().Where(a => a.Id > x1 && a.Id < x2).ToList();
                if (listcommand.Count.Equals(0)) return false;
                List<bool> counter = new List<bool>();
                for (int i = 0; i < listcommand.Count; i++)
                {
                    if (listcommand[i].GuidResult.Trim().Equals(listcommand[i].ComandGuidResult.Trim())) counter.Add(true);
                }
                return counter.Count.Equals(listcommand.Count);
            }
            catch (Exception)
            {
                return false;
            }

        }


        public static int UpdateCommandRecordGuidResult(string guidresult, string guid)
        {
            return FreeSqlDB.fsql00.Update<CommandRecord>().Set(a => a.GuidResult, guidresult).Where(a => a.Guid.Equals(guid)).ExecuteAffrows();
        }

        public static bool ExistCommandGUIDString(string GUID)
        {
            return FreeSqlDB.fsql00.Select<CommandRecord>().Where(a => a.Guid.Equals(GUID)).ToList().Count > 0;
        }


    }
}
